﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using EPiServer.Framework;

using EPiServer.Shell;
using EPiServer.Shell.Dashboard;
using EPiServer.Shell.Gadgets;
using System.Collections.Generic;




namespace EPiServer.Research.MobileCenter.Core
{
    
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class MobileCenterGadgetsInitializer : IInitializableModule
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");          
            routes.MapRoute("Gadget", "gadgets/{gadgetname}", new { controller = "MobileCenterDashboard", action = "GetGadgetByName", gadgetname = (string)null });
        }
       
        #region IInitializableModule Members

        /// <summary>
        /// Initializes the specified context. Adds routes and adds delegates to when the dashboard is loaded and when gadgets are loaded.
        /// </summary>
        /// <param name="context">The context.</param>
        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            RegisterRoutes(RouteTable.Routes);
            IDashboardRepository dashboardRepository = ShellContext.Instance.Container.GetExportedValue<IDashboardRepository>();
            dashboardRepository.Loaded += new EventHandler<DashboardEventArgs>(dashboardRepository_Loaded);
        }

        public void Preload(string[] parameters)
        {          
        }

        public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {            
        }

        #endregion

        /// <summary>
        /// Handles the Loaded event of the dashboardRepository control. Add a tab and then dont show it
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EPiServer.Shell.Dashboard.DashboardEventArgs"/> instance containing the event data.</param>
        private void dashboardRepository_Loaded(object sender, DashboardEventArgs e)
        {
            string path = VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Request.Path);
         
            // We want to hide the MobileCenter tab if the request is for the dashboard.
            if (path.Equals(Paths.ProtectedRootPath, StringComparison.OrdinalIgnoreCase) ||
                path.Equals(Paths.ProtectedRootPath + Paths.ShellModuleName + "/" , StringComparison.OrdinalIgnoreCase) ||
                path.Equals(Paths.ProtectedRootPath + Paths.ShellModuleName + "/Dashboard/", StringComparison.OrdinalIgnoreCase))
            {
                foreach (Tab tab in e.Dashboard.Tabs.ToList())
                {
                    if (tab.Name == GadgetUtil.MobileCenterTabName)
                    {
                        e.Dashboard.Tabs.Remove(tab);
                        break;
                    }
                }
            }
        }
    }
}